﻿using System;
using System.Diagnostics;
using System.Reflection;
using System.Windows.Forms;
using VistaKeysExtender.Properties;

namespace VistaKeysExtender
{
    public partial class Form : System.Windows.Forms.Form
    {
        #region User Interface

        private bool IsLoaded { get; set;}

        public Form()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            lblVersion.Text = string.Format("v. {0}", Assembly.GetExecutingAssembly().GetName().Version);
            LoadData();
        }

        protected override void OnActivated(EventArgs e)
        {
            base.OnActivated(e);

            if (!IsLoaded)
            {
                IsLoaded = true;
                Hide();
            }
        }

    	#region Menu

    	private void CloseToolStripMenuItemClick(object sender, EventArgs e)
    	{
    		Close();
    	}

    	private void ShowToolStripMenuItemClick(object sender, EventArgs e)
    	{
    		ShowWindow();
    	}

    	#endregion

        private void NotifyIconMouseDoubleClick(object sender, MouseEventArgs e)
        {
        	ShowWindow();
        }

    	#region Window

    	private void BtnOkClick(object sender, EventArgs e)
    	{
    		HideWindow();
    		SaveData();
    		MessageBox.Show(this, @"Restart application to take affect.", @"Keys Extenders");
    	}

    	private void BtnCancelClick(object sender, EventArgs e)
    	{
    		HideWindow();
    		LoadData();
    	}

    	private void CheckBoxMEnabledCheckedChanged(object sender, EventArgs e)
    	{
    		UpdateStatusMovedControls();
    	}

    	private void CheckBoxAEnabledCheckedChanged(object sender, EventArgs e)
    	{
    		UpdateStatusAttachedControls();
    	}

    	private static void LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
    	{
    		Process.Start(((LinkLabel)sender).Text);
    	}

    	#endregion

		private void HideWindow()
		{
			Hide();
		}

		private void ShowWindow()
		{
			WindowState = FormWindowState.Normal;
			Show();
			Activate();
		}

        private void UpdateStatusMovedControls()
        {
            label7.Enabled =
                numericUpDownmStep.Enabled =
                checkBoxMAlt.Enabled =
                checkBoxMCtrl.Enabled =
                checkBoxMShift.Enabled = label6.Enabled = label5.Enabled = checkBoxMEnabled.Checked;
        }

        private void UpdateStatusAttachedControls()
        {
            checkBoxAAlt.Enabled =
                checkBoxACtrl.Enabled =
                checkBoxAShift.Enabled = label3.Enabled = label4.Enabled = checkBoxAEnabled.Checked;
        }

        #endregion

        #region Settings Data

        private void LoadData()
        {
            checkBoxAEnabled.Checked = Settings.Default.WindowsAttachedEnable;
            checkBoxMEnabled.Checked = Settings.Default.WindowsMoveEnable;
            checkBoxAAlt.Checked = Settings.Default.WindowsAttachedAlt;
            checkBoxAShift.Checked = Settings.Default.WindowsAttachedShift;
            checkBoxACtrl.Checked = Settings.Default.WindowsAttachedCtrl;
            checkBoxMAlt.Checked = Settings.Default.WindowsMoveAlt;
            checkBoxMShift.Checked = Settings.Default.WindowsMoveShift;
            checkBoxMCtrl.Checked = Settings.Default.WindowsMoveCtrl;
            numericUpDownmStep.Value = Settings.Default.WindowsMoveStep;
            checkBoxSEnabled.Checked = Settings.Default.WindowsScreenEnable;
            checkBoxSAlt.Checked = Settings.Default.WindowsScreenAlt;
            checkBoxSShift.Checked = Settings.Default.WindowsScreenShift;
            checkBoxSCtrl.Checked = Settings.Default.WindowsScreenCtrl;
            UpdateStatusMovedControls();
            UpdateStatusAttachedControls();
        }

        private void SaveData()
        {
            Settings.Default.WindowsAttachedEnable = checkBoxAEnabled.Checked;
            Settings.Default.WindowsMoveEnable = checkBoxMEnabled.Checked;
            Settings.Default.WindowsAttachedAlt = checkBoxAAlt.Checked;
            Settings.Default.WindowsAttachedShift = checkBoxAShift.Checked;
            Settings.Default.WindowsAttachedCtrl = checkBoxACtrl.Checked;
            Settings.Default.WindowsMoveAlt = checkBoxMAlt.Checked;
            Settings.Default.WindowsMoveShift = checkBoxMShift.Checked;
            Settings.Default.WindowsMoveCtrl = checkBoxMCtrl.Checked;
            Settings.Default.WindowsMoveStep = (int)numericUpDownmStep.Value;
            Settings.Default.WindowsScreenEnable = checkBoxSEnabled.Checked;
            Settings.Default.WindowsScreenAlt = checkBoxSAlt.Checked;
            Settings.Default.WindowsScreenShift = checkBoxSShift.Checked;
            Settings.Default.WindowsScreenCtrl = checkBoxSCtrl.Checked;
            Settings.Default.Save();
        }

        #endregion
    }
}
